﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using CPPEI.Coolzon.Common;
using System.Xml;

namespace CPPEI.Coolzon.Metadata.ParserV1
{
    public class SolutionListXmlParser : ISolutionListXmlParser
    {
        public List<SolutionRef> LoadSolutionList(XmlDocument xDoc)
        {
            if (xDoc == null) return null;
            try
            {
                XmlNode xSolution = xDoc.SelectSingleNode("Solution-List");
                if (xSolution==null) return null;

                List<SolutionRef> result = new List<SolutionRef>();

                XmlNodeList xNodeSoulutions = xSolution.SelectNodes("Solution");
                foreach (XmlNode xNodeSolution in xNodeSoulutions)
                {
                    SolutionRef solutionRef = new SolutionRef();
                    solutionRef.RuntimeVersion = Version.Parse(xNodeSolution.GetXAttributeValue("RuntimeVersion"));
                    solutionRef.Version = Version.Parse(xNodeSolution.GetXAttributeValue("Version"));
                    solutionRef.Name = xNodeSolution.GetXAttributeValue("Name");
                    solutionRef.Type = xNodeSolution.GetXAttributeValue("Type");
                    solutionRef.Caption = xNodeSolution.GetXAttributeValue("Caption");
                    solutionRef.PreviewImageUrl = xNodeSolution.GetXAttributeValue("PreviewImageUrl");

                    result.Add(solutionRef);
                }
                return result;
            }
            catch(Exception ex)
            {
                Logger.ErrorToTag(this.GetType().ToString(), ex.Message);
            }
            return null;
        }
    }
}
